<!--
 * @Author: JiraiyaChen 979526234@qq.com
 * @Date: 2025-07-07 14:02:11
 * @LastEditors: JiraiyaChen 979526234@qq.com
 * @LastEditTime: 2025-07-17 09:51:43
 * @Description: 竞争性磋商
-->

<template>
  <div>
    <div class="title">
      <span>
        {{ componentName }}
      </span>
    </div>
    <el-descriptions :column="1" border>
      <el-descriptions-item label="项目名称">
        {{ staticData.projectName }}
      </el-descriptions-item>
      <el-descriptions-item label="项目编号">
        {{ staticData.projectNumber }}
      </el-descriptions-item>
      <el-descriptions-item label="邀请供应商参加采购活动的具体方式和相关情况">
        <el-checkbox-group v-model="formData.mediaNameSelect">
          <el-checkbox v-for="item in mediaNameList" :key="item" :label="item" :value="item">{{ item }}</el-checkbox>
        </el-checkbox-group>
        <el-input
          v-model="formData.mediaNameInput"
          placeholder="请填写招标公告刊登的媒体名称"
          size="normal"
          clearable
        />
      </el-descriptions-item>
      <el-descriptions-item label="响应文件开启日期">
        {{ staticData.bidTime }}
      </el-descriptions-item>
      <el-descriptions-item label="磋商地点">
        {{ staticData.bidPlace }}
      </el-descriptions-item>
      <el-descriptions-item label="磋商小组根据评审标准">
        磋商采用综合评分法，即在响应文件满足磋商文件全部实质性要求且按评审因素的量化指标评审得分最高的供应商为成交供应商，具体详见采购文件。
      </el-descriptions-item>
      <el-descriptions-item label="磋商小组成员名单">
        {{ staticData.judgeInfo }}
      </el-descriptions-item>
    </el-descriptions>
    <el-descriptions direction="vertical" :column="1" border>
      <el-descriptions-item class-name="hidden-value" label-class-name="center-label" label="评审情况记录和说明" />
    </el-descriptions>
    <el-descriptions :column="1" border>
      <el-descriptions-item class-name="des-table" label="无效响应供应商名单">
        <CommonTable :table-data="staticData.invalidRecords" :columns="invalidRecordsColumns" />
      </el-descriptions-item>
    </el-descriptions>
    <el-descriptions :column="1" border>
      <el-descriptions-item class-name="des-table" label="报价情况">
        <div class="table-width">
          <el-table border :data="staticData.quotationSituation" style="width: 100%">
            <el-table-column align="center" header-align="center" type="index" label="序号" width="80" />
            <el-table-column
              align="center"
              header-align="center"
              prop="supplierName"
              label="供应商名称"
              width="150px"
            />
            <el-table-column align="center" header-align="center" label="响应文件密封完好性" width="200px" />
            <el-table-column align="center" header-align="center" label="磋商次序" width="150px" />
            <el-table-column align="center" header-align="center" prop="bidPrice" label="第1次报价" width="150px" />
            <!-- 动态报价列 -->
            <el-table-column
              v-for="(item, index) in dynamicQuotationColumn"
              :key="item.id"
              width="150px"
              :label="`第${index + 2}次报价`"
              align="center"
              header-align="center"
            >
              <template #default="{ row }">
                {{ getPriceList(row.projectId, index) }}
              </template>
            </el-table-column>
            <!-- 评审列 itemList -->
            <el-table-column v-for="item in reviewColumn" :key="item.id" :label="item.itemName" width="150px">
              <template #default>
                {{ getReviewVal(item) }}
              </template>
            </el-table-column>
            <el-table-column v-if="comType === '谈判'" prop="totalScore" label="合格性" width="150px">
              <template #default="{ row }">
                {{ getQualified(row) }}
              </template>
            </el-table-column>
            <el-table-column v-else prop="totalScore" label="总分" width="150px" />

            <el-table-column label="备注" width="150px" />
          </el-table>
        </div>
      </el-descriptions-item>
    </el-descriptions>
    <el-descriptions :column="1" border>
      <el-descriptions-item class-name="des-table" :label="`标段号： ${staticData.projectNumber}`">
        <div class="table-width">
          <CommonTable :table-data="staticData.sectionNumberList" :columns="sectionNumberListColumns" />
        </div>
      </el-descriptions-item>
    </el-descriptions>
    <el-descriptions :column="1" border>
      <el-descriptions-item label="废标原因">
        {{ staticData.deprecatedReason }}
      </el-descriptions-item>
    </el-descriptions>
    <el-descriptions :column="1" border>
      <el-descriptions-item label="磋商小组签字签字" />
      <el-descriptions-item
        label="其他需要说明的情况，包括磋商过程中磋商响应单位根据评标委员会要求进行的澄清、说明或者补正，评标委员会成"
      >
        <el-input
          v-model="formData.bidRemark"
          placeholder="请填写其他需要说明的情况"
          size="normal"
          type="textarea"
          :rows="5"
          clearable
        />
      </el-descriptions-item>
      <el-descriptions-item label="采购人采购结果确认">
        我单位同意确定排序第一的中标候选人为中标单位；
        <el-radio-group v-model="formData.baseRank">
          <el-radio value="是" label="是">是</el-radio>
          <el-radio value="否" label="否">否</el-radio>
        </el-radio-group>
        <el-input
          v-model="formData.baseRankRemark"
          placeholder="请填写采购人采购结果确认"
          size="normal"
          type="textarea"
          :rows="5"
          clearable
        />
        采购人代表签字：
      </el-descriptions-item>
    </el-descriptions>
    <div class="flex items-center justify-center mt-4">
      <el-button type="primary" @click="handleSave">提交</el-button>
    </div>
  </div>
</template>
<script setup lang="ts">
import CommonTable from './CommonTable.vue'
import {
  handleMediaNameSelect,
  mediaNameList,
  transformInvalidRecords,
  transformSectionNumberList,
  transformTime
} from './data'
import { getReviewReportDynamicValue, updateReviewReportDynamicValue } from '@/api/review-report-generation'
const props = defineProps({
  componentName: {
    type: String,
    default: ''
  },
  comType: {
    type: String,
    default: ''
  }
})
const invalidRecordsColumns = [
  {
    prop: 'supplierNames',
    label: '供应商名称'
  },
  {
    prop: 'comment',
    label: '原因'
  }
]
const sectionNumberListColumns = [
  {
    prop: 'supplierName',
    label: '成交候选供应商名称'
  },
  {
    prop: 'rankNo',
    label: '成交候选排序'
  },
  {
    prop: 'reviewPrice',
    label: '成交价格'
  },
  {
    prop: 'comment',
    label: '理由'
  }
]
onMounted(() => {
  initData()
})
// 动态报价列
const dynamicQuotationColumn = ref<any[]>([])
// 评审列 itemList
const reviewColumn = ref<any[]>([])
// 符合性打分
const fitResultListRes = ref<any[]>([])
// 直接打分
const directResultListRes = ref<any[]>([])
//获取符合性打分 和 直接打分
const getResultList = (arr: any) => {
  if (Array.isArray(arr) && arr.length > 0) {
    const { directResultList, fitResultList } = arr[0]
    fitResultListRes.value = fitResultList
    directResultListRes.value = directResultList
  }
}
const initData = async () => {
  const res: any = await getReviewReportDynamicValue()
  getResultList(res.winBidResult)
  dynamicQuotationColumn.value = res.appendBatchs
  reviewColumn.value = res.itemList
  formData.value.mediaNameSelect = handleMediaNameSelect(res.mediaNameSelect)
  formData.value.mediaNameInput = res.mediaNameInput
  formData.value.baseRank = res.baseRank
  formData.value.baseRankRemark = res.baseRankRemark
  formData.value.bidRemark = res.bidRemark

  staticData.value.projectName = res.projectName
  staticData.value.projectNumber = res.projectNumber
  staticData.value.bidTime = transformTime(res.bidTime)
  staticData.value.bidPlace = res.bidPlace
  staticData.value.invalidRecords = res.invalidRecords
  staticData.value.deprecatedReason = res.deprecatedReason
  staticData.value.judgeInfo = res.judgeInfo
  // 报价情况
  staticData.value.quotationSituation = transformInvalidRecords(res)
  // 标段号
  staticData.value.sectionNumberList = transformSectionNumberList(res.deprecated, res.winBidResult)
}
const formData = ref({
  mediaNameSelect: [], // 邀请供应商参加采购活动的具体方式和相关情况
  mediaNameInput: '', // 邀请供应商参加采购活动的具体方式和相关情况
  bidRemark: '', // 其他需要说明的情况
  baseRank: '', // 采购人采购结果确认
  baseRankRemark: '' // 采购人采购结果确认
})
const staticData = ref({
  projectName: '', // 项目名称
  projectNumber: '', // 项目编号
  bidTime: '', // 响应文件开启日期
  bidPlace: '', // 磋商地点
  invalidRecords: [], // 无效响应供应商名单
  deprecatedReason: '', // 废标原因
  judgeInfo: '', // 磋商小组成员名单
  quotationSituation: [], // 报价情况
  sectionNumberList: [] // 标段号
})
const getReviewVal = ({ id, markModeName }) => {
  if (markModeName === '直接打分') {
    const res: any = directResultListRes.value.find((item) => item.itemId === id)
    return res.score
  }
  if (markModeName === '符合性打分') {
    const res: any = fitResultListRes.value.find((item) => item.itemId === id)
    return res.fitResult
  }
  return '-'
}
// 谈判的时候 如果前面都是通过 则合格性为通过 否则为不通过
const getQualified = (row: any) => {
  const { fitResultList } = row
  const res = fitResultList.every((item) => item.fitResult === '通过')
  return res ? '合格' : '不合格'
}
// 获取报价
const getPriceList = (projectId: string, index: number) => {
  try {
    const res: any = dynamicQuotationColumn.value.filter((item) => item.projectId === projectId)
    return res[index].appendPrice
  } catch (error) {
    return '-'
  }
}
const handleSave = async () => {
  const apiData = cloneDeep(formData.value)
  apiData.mediaNameSelect = apiData.mediaNameSelect.join(',')
  await updateReviewReportDynamicValue(apiData)
  await initData()
}
</script>
<style lang="scss" scoped>
@import './index.scss';
</style>
